<template>
	<div class=""></div>
</template>

<script setup lang="ts">
import { CaptchaType } from './types'
import { PropType } from 'vue'

const props = defineProps({
	type: {
		type: String as PropType<CaptchaType>,
		default: ''
	}
})

let captcha: any

const initCaptcha = () => {
	const captchaId =
		props.type === 'login'
			? import.meta.env.VITE_CAPTURE_LOGIN
			: import.meta.env.VITE_CAPTURE_REGISTER
	;(window as any).initGeetest4?.(
		{
			captchaId,
			product: 'bind'
		},
		function (val: any) {
			captcha = val
		}
	)
}

initCaptcha()

// 先把geetest在测试环境关闭 UP-485
const OPEN_GEETEST = false

const getValidate = () =>
	new Promise((resolse, reject) => {
		if (OPEN_GEETEST) {
			captcha
				.onReady(function () {
					//验证码ready之后才能调用showCaptcha方法显示验证码
				})
				.onSuccess(function () {
					const { captcha_output, gen_time, lot_number, pass_token } = captcha.getValidate()
					resolse({
						captchaOutput: captcha_output,
						genTime: gen_time,
						lotNumber: lot_number,
						passToken: pass_token
					})
					//your code,结合您的业务逻辑重置验证码
					// captcha.reset()
				})
				.onError(function () {
					console.log('onError')
					reject()
					//your code
				})
			captcha.showCaptcha()
		} else {
			resolse({
				captchaOutput: 'lcFOuTHxAc7X4vc_mWtGj9mWk-wgcE4fF4RxKQ5kTgoIvpbnnAZfWN49K79OEMuoPHzZ5d8NPG',
				genTime: '1729674100',
				lotNumber: '8fee2c74467242f4b7f2540a5813a9d5',
				passToken: 'fcdd2fffcf7760141c668dd6166861b7cf9f763ffa0c2f3458323e97bf7764b9'
			})
		}
	})

defineExpose({ getValidate })
</script>
